local function SarfFix2_SecureActionButton_OnDragStart(frame)
	if InCombatLockdown() or ( frame.locked ) or ( ( type(frame.db) == "table" ) and frame.db.locked ) then return end
	frame:StartMoving()
end

local function SarfFix2_SecureActionButton_OnDragStop(frame)
	frame:StopMovingOrSizing()
	frame.x, frame.y = frame:GetCenter()
	if ( type(frame.db) == "table" ) then
		frame.db.x = frame.x;
		frame.db.y = frame.y;
	end
end

local function SarfFix2_SecureActionButton_SetDB(frame, newdb)
	frame.db = newdb;
	frame:Position()
end

local function SarfFix2_SecureActionButton_Position(frame)
	frame:ClearAllPoints()
	frame:SetScale(db.scale)
	local x,y;
	local doSet = true;
	if ( type(frame.db) == "table" ) then
		x = frame.db.x;
		y = frame.db.y;
		doSet = x and y;
	end
	if ( doSet ) then
		x = frame.x;
		y = frame.y;
	end
	frame:SetPoint("CENTER", UIParent, x and "BOTTOMLEFT" or "CENTER", x or 0, y or -100)
end

local function SarfFix2_SecureActionButton_SetText(frame, ...)
	frame.text:SetText(...)
end

local function SarfFix2_SecureActionButton_Reize(frame)
	frame:SetWidth(text:GetStringWidth() + frame.resizePadding)
end

local function SarfFix2_SecureActionButton_SetTooltip(frame, tip)
	frame.tiptext = tip
end

local function SarfFix2_SecureActionButton_OnLeave(frame)
	if ( frame.tooltip ) then
		frame.tooltip:Hide();
		frame.tooltip = nil;
	end
end

local function SarfFix2_SecureActionButton_OnEnter(frame)
	if ( type(frame.db) ~= "table" ) or not frame.db.char.showtooltip or not frame.tiptext then return end
	local sx, sy, x, y = GetScreenHeight(), GetScreenWidth(), self:GetCenter()
	local x1, y1, y2 = "RIGHT", "TOP", "BOTTOM"
	if x < (sx/2) then x1 = "LEFT" end
	if y < (sy/2) then y1, y2 = y2, y1 end
	local tooltip = GameTooltip;
	frame.tooltip = tooltip;
 	tooltip:SetOwner(self, "ANCHOR_NONE")
	tooltip:SetPoint(y1..x1, self, y2..x1)
	tooltip:SetText(frame.tiptext)
end

function SarfFix2:CreateSecureActionButton(name)

	------------------------------
	--      Are you local?      --
	------------------------------
	
	--------------------------
	--      Main Frame      --
	--------------------------
	
	local frame = CreateFrame("Button", name, UIParent, "SecureActionButtonTemplate")
	frame:SetHeight(24)
	
	frame:EnableMouse(true)
	frame:RegisterForClicks("AnyUp")
	frame:SetMovable(true)
	frame:RegisterForDrag("LeftButton")
	frame:SetClampedToScreen(true)
	
	frame:SetBackdrop({
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		edgeSize = 16,
		insets = {left = 5, right = 5, top = 5, bottom = 5},
		tile = true, tileSize = 16,
	})
	frame:SetBackdropColor(0.09, 0.09, 0.19, 0.5)
	frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 0.5)
	
	frame:SetScript("OnDragStart", SarfFix2_SecureActionButton_OnDragStart);
	
	frame:SetScript("OnDragStop", SarfFix2_SecureActionButton_OnDragStop);
	
	frame.SetDB = SarfFix2_SecureActionButton_SetDB;

	frame.Position = SarfFix2_SecureActionButton_Position;
	
	--------------------
	--      Text      --
	--------------------
	
	local text = frame:CreateFontString(nil, nil, "GameFontNormalSmall")
	text:SetPoint("CENTER")
	frame.text = text;

	frame.SetText = SarfFix2_SecureActionButton_SetText;

	frame.resizePadding = 8;
	frame.Resize = SarfFix2_SecureActionButton_Resize;
	
	
	-----------------------
	--      Tooltip      --
	-----------------------
	
	frame.SetTooltip = SarfFix2_SecureActionButton_SetTooltip;
	
	
	frame:SetScript("OnLeave", SarfFix2_SecureActionButton_OnLeave);
	frame:SetScript("OnEnter", SarfFix2_SecureActionButton_OnEnter);
	return frame;
end